from rest_framework.generics import ListAPIView,UpdateAPIView
from apps.orders.models import OrderInfo, OrderGoods
from apps.meiduo_admin.serializers.order import OrderInfoSerializer
from apps.meiduo_admin.utils import PageNum
from rest_framework.views import APIView
from rest_framework.response import Response


class OrderInfoView(ListAPIView):
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword is '' or keyword is None:
            return OrderInfo.objects.all()
        else:
            return OrderInfo.objects.filter(order_id__contains=keyword)

    serializer_class = OrderInfoSerializer
    pagination_class = PageNum


class OrderDetailView(APIView):
    def get(self,request,order_id):
        order = OrderInfo.objects.get(order_id=order_id)
        s = OrderInfoSerializer(instance=order)
        return Response(s.data)


class OrderStatusView(APIView):
    def put(self,request,order_id):
        order = OrderInfo.objects.get(order_id=order_id)
        status = request.data.get('status')
        if status not in [OrderInfo.ORDER_STATUS_ENUM['UNPAID'],OrderInfo.ORDER_STATUS_ENUM['UNSEND'],OrderInfo.ORDER_STATUS_ENUM['UNRECEIVED'],OrderInfo.ORDER_STATUS_ENUM['UNCOMMENT'],OrderInfo.ORDER_STATUS_ENUM['FINISHED']]:
            return Response({'msg':'订单状态参数值错误'})
        order.status = status
        order.save()
        return Response({
            'order_id':order_id,
            'status':status
        })

